# 支持IF(A) ELSE()的写法
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
# 支持相对路径
set(CMAKE_SUPPRESS_REGENERATION 1)
set(CMAKE_USE_RELATIVE_PATHS ON)
set(BUILD_USE_64BITS on)			# 默认是64位

IF(CMAKE_CL_64)
	set(PLATFORM x64)
	message(STATUS "Windows_x64")
ELSE(CMAKE_CL_64)
	set(PLATFORM x32)
	message(STATUS "Windows_x32")
ENDIF()

message(STATUS "build PLATFORM: ${PLATFORM}")

set(CRT_VERSION_NAME)

IF(CMAKE_GENERATOR MATCHES "Visual Studio 10 2010")
	set(CRT_VERSION_NAME "vc100")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 11 2012")
	set(CRT_VERSION_NAME "vc110")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 12 2013")
	set(CRT_VERSION_NAME "vc120")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 14 2015")
	set(CRT_VERSION_NAME "vc140")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 15 2017")
	set(CRT_VERSION_NAME "vc141")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 16 2019")
	set(CRT_VERSION_NAME "vc142")
ELSEIF(CMAKE_GENERATOR MATCHES "Visual Studio 17 2022")
	set(CRT_VERSION_NAME "vc143")
ELSE(CMAKE_GENERATOR MATCHES "Visual Studio 16 2019")
	set(CRT_VERSION_NAME "GNU")
	set(CMAKE_XX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--rpath=./ -Wl,--rpath=./Models -Wl,--rpath=./thirdparty/lib -Wl,--rpath=../thirdparty/lib -Wl,--rpath=../")
ENDIF()

message(STATUS "CRT Version Name : ${CRT_VERSION_NAME}")


set(CMAKE_DEBUG_POSTFIX "_d")
message(STATUS "DEBUG_POSTFIX .._d..")

IF(CMAKE_GENERATOR MATCHES "Visual Studio")
	MESSAGE(STATUS "compile_options    /MP")
	add_compile_options(/MP /Zi /utf-8)	
ENDIF()

#IF(CMAKE_BUILD_TYPE MATCHES "DEBUG")
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "$ENV{ROOT_DIR}/bin")#可执行文件输出目录
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "$ENV{ROOT_DIR}/lib")#静态库输出目录
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "$ENV{ROOT_DIR}/bin")#动态库输出目录
	message(STATUS "build debug .....")
#ELSEIF(CMAKE_BUILD_TYPE MATCHES "RELEASE")
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "$ENV{ROOT_DIR}/bin")#可执行文件输出目录
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE  "$ENV{ROOT_DIR}/lib")#静态库输出目录
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "$ENV{ROOT_DIR}/bin")#动态库输出目录
	message(STATUS "build Release .....")
#ENDIF()


#------------------------------------------------------------
#-- 拷贝文件夹，当文件不同的时候
#-- 使用限制：因为是构建时确定拷贝对象，因此无法拷贝ui_*.h
#-- 使用方式：COPY_DIRECTORY_IF_DIFFERENCE(${PROJDIR}/include *.h ${dependenceInclude})
#------------------------------------------------------------
function(COPY_DIRECTORY_IF_DIFFERENCE srcDir expressions dstDir)
    set(COPYFILE_EXE $ENV{CMAKELIST_TXT_PATH}/SetupAccu/VersionManage/Manteia.Util.CopyFile.CSharp.exe)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 
        COMMAND ${COPYFILE_EXE} -copy_diff -src ${srcDir} -pattern ${expressions} -dst ${dstDir}
    )
endfunction(COPY_DIRECTORY_IF_DIFFERENCE)

# -------------------------------- 根据文件夹进行分组 --------------------------------- #
macro(group_src_by_dir src_files)
  foreach(file_path ${${src_files}})
    string(REGEX REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/\(.*\) \\1 relative_path
                         ${file_path})
    string(REGEX REPLACE "(.+)\\..\\..*" \\1 relative_group ${relative_path})
    list(FIND c_groups ${relative_group} index)
    if(NOT index LESS 0)
      string(REPLACE "/" "\\" relative_group ${relative_group})
      source_group(${relative_group} FILES ${file_path})
    else()
      string(REGEX MATCH "(.+)/.*" has_group ${relative_path})
      if(has_group)
        string(REGEX REPLACE "(.+)/.*" \\1 group ${relative_path})
        string(REPLACE "/" "\\" group ${group})
        source_group(${group} FILES ${file_path})
      else()
        source_group("" FILES ${file_path})
      endif()
    endif()
  endforeach(file_path)
endmacro(group_src_by_dir)

MACRO(QT_VTK_Project project_name generate_method)

	set(CMAKE_PREFIX_PATH $ENV{Qt5_DIR})
	find_package(Qt5 COMPONENTS ${QT_LIBRARY_LIST} REQUIRED)
	if(Qt5_FOUND)
		message(STATUS "+ found Qt5_DIR:${Qt5_DIR}")
		file(TO_CMAKE_PATH "$ENV{Qt5_DIR}/bin" Qt5_BINS_DIR)
		message(STATUS "* found Qt_bin_DIRS-${Qt5_BINS_DIR}") # 这里是空的
	else(Qt5_FOUND)
		message(FATAL_ERROR "- Qt5 not found. Please set Qt_DIR")
	endif(Qt5_FOUND)
	
	find_package(VTK REQUIRED) 
	if(VTK_FOUND)
		message(STATUS "+ found VTK_DIR-${VTK_DIR}")
		message(STATUS "* found VTK_INCLUDE_DIRS-${VTK_INCLUDE_DIRS}")
		include(${VTK_USE_FILE})
		include_directories(${VTK_INCLUDE_DIRS})
		message(STATUS "$ENV{VTK_DIR}/lib")
		file(TO_CMAKE_PATH "$ENV{VTK_DIR}/lib" VTK_LIBS_DIR)
		file(TO_CMAKE_PATH "$ENV{VTK_DIR}/bin" VTK_BINS_DIR)
		message(STATUS "* found VTK_LIB_DIRS-${VTK_LIBRARY_DIRS}") # 这里是空的
		message(STATUS "* found VTK_LIBS-${VTK_LIBRARIES}") # 这里只有名字
	else(VTK_FOUND)
		message(FATAL_ERROR "- VTK not found. Please set VTK_DIR")
	endif(VTK_FOUND)
	
	#启动自动MOC编译
	set(CMAKE_AUTOMOC ON)
	#启动自动UIC编译
	set(CMAKE_AUTOUIC ON)
	#启用自动QRC编译
	set(CMAKE_AUTORCC ON)
	set(CMAKE_INCLUDE_CURRENT_DIR ON)
	#将各种文件归类到指定变量
	FILE(GLOB_RECURSE HEADER_FILES "*.h")
	FILE(GLOB_RECURSE SRC_FILES "*.cpp")
	FILE(GLOB_RECURSE FORMS "*.ui")
	FILE(GLOB_RECURSE RESOURCE_FILES "*.qrc")
	FILE(GLOB TS_FILES "*.ts")
	FILE(GLOB MOC_FILES "moc_*.cpp")
	FILE(GLOB_RECURSE cmakefile "*.txt")

	#将ui文件编译成ui_*.h文件
	IF(FORMS)
		QT5_WRAP_UI(FORMS_UIC ${FORMS})
	ENDIF()

	#将包含Q_OBJECT的头文件编译成moc_*.cpp文件
	QT5_WRAP_CPP(MOC_FILES ${HEADER_FILES})

	#启用qrc资源文件
	QT5_ADD_RESOURCES(RES_FILES ${RESOURCE_FILES})
	#将MOC,UI,QRC的中间生成文件编译进EXE中
	SOURCE_GROUP("Generated Files" FILES ${MOC_FILES} ${TS_FILES})

	set(FromGroup_FileLists ${FORMS} ${HEADER_FILES} ${SRC_FILES} ${cmakefile})
	group_src_by_dir(FromGroup_FileLists)
	
	#添加qt生成的头文件
	include_directories($ENV{BUILD_DIR_PATH}/${project_name})
	#添加自定义头文件
	include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)
	message(STATUS "Add Myself headfiles ：${CMAKE_CURRENT_SOURCE_DIR}/../ ")
	include_directories(${CMAKE_CURRENT_SOURCE_DIR})
	message(STATUS "Add Myself headfiles ：${CMAKE_CURRENT_SOURCE_DIR}")
	include_directories(${CMAKE_SOURCE_DIR})
	include_directories($ENV{ROOT_DIR}/include)
	
	#静态库**文件夹**
	link_directories(${CMAKE_RUNTIME_LIBRARY_DIRECTORY} ${CMAKE_RUNTIME_ARCHIVE_DIRECTORY} $ENV{ROOT_DIR})
	link_directories(${VTK_LIBS_DIR})
	#set(CMAKE_INSTALL_PREFIX "$ENV{ROOT_DIR}/install")#安装目录前缀
	# 生成物
	add_compile_options("/utf-8" )
	message(STATUS "start building----------------------------------")
	IF(${generate_method} MATCHES EXE)
		ADD_EXECUTABLE(${project_name} ${HEADER_FILES} ${SRC_FILES} ${MOC_FILES} ${FORMS_UIC} ${RESOURCE_FILES} ${RES_FILES})
		message(STATUS "[${project_name}]项目EXE")
	ELSEIF(${generate_method} MATCHES LIB)
		ADD_LIBRARY(${project_name} STATIC ${HEADER_FILES} ${SRC_FILES} ${MOC_FILES} ${FORMS_UIC} ${RESOURCE_FILES} ${RES_FILES})
		message(STATUS "[${project_name}]项目LIB")
	ELSEIF(${generate_method} MATCHES DLL)
		ADD_LIBRARY(${project_name} SHARED ${HEADER_FILES} ${SRC_FILES} ${MOC_FILES} ${FORMS_UIC} ${RESOURCE_FILES} ${RES_FILES})
		message(STATUS "[${project_name}]项目DLL")
	ELSE()
		message(FATAL_ERROR "[${project_name}]项目生成方式出现异常----------------------------------")
	ENDIF()
		message(STATUS "end building----------------------------------")
	
	#静态库**文件**
	if(CMAKE_BUILD_TYPE MATCHES "DEBUG")
		file(GLOB_RECURSE Lib_FILES "$ENV{ROOT_DIR}/*_d.lib")
		file(GLOB_RECURSE VTK_Lib_FILES "${VTK_LIBS_DIR}/*_d.lib")
	elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE")
		file(GLOB_RECURSE Lib_FILES "$ENV{ROOT_DIR}/*.lib")
		file(GLOB_RECURSE VTK_Lib_FILES "${VTK_LIBS_DIR}/*.lib")
	endif()
	target_link_libraries(${project_name}  ${Lib_FILES})
	target_link_libraries(${project_name}  ${VTK_Lib_FILES})
	foreach(library ${QT_LIBRARY_LIST})
		target_link_libraries( ${project_name} Qt5::${library})
		message(STATUS "target_link Qt library:Qt5::${library}")
	endforeach()
	
	#设置信息
	if(${generate_method} MATCHES EXE)
			set_target_properties(${project_name} PROPERTIES LINK_FLAGS " /level='requireAdministrator'")
			message(STATUS"add LINK_FLAGS: /level='requireAdministrator")
	endif()	
	set_target_properties(${project_name} PROPERTIES LINK_FLAGS " /DEBUG:FULL")
	set_target_properties(${project_name} PROPERTIES VS_DEBUGGER_ENVIRONMENT "${Qt5_BINS_DIR};${VTK_BINS_DIR};")
	set_target_properties(${project_name} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "$ENV{ROOT_DIR}/bin")
	
ENDMACRO(QT_VTK_Project)